package com.apps.ftapp.activity;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.annotation.IdRes;

import com.apps.ftapp.R;
import com.apps.ftapp.adapter.MyErrorSubjectAdapter;
import com.apps.ftapp.model.MyErrorModel;
import com.apps.ftapp.util.ActivitySkipUtil;
import com.apps.ftapp.util.Async;
import com.apps.ftapp.util.Constant;
import com.apps.ftapp.util.L;
import com.apps.ftapp.util.StatusBarUtils;
import com.apps.ftapp.util.Tools;
import com.google.gson.Gson;
import com.jaeger.library.StatusBarUtil;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cz.msebera.android.httpclient.Header;
import info.hoang8f.android.segmented.SegmentedGroup;

/**
 * 我的错题
 */
public class MyErrorSubjectActivity extends Activity implements View.OnClickListener {


    @BindView(R.id.listView)
    ListView listView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.wodecuoti)
    RadioButton wodecuoti;
    @BindView(R.id.tuijiancuoti)
    RadioButton tuijiancuoti;
    @BindView(R.id.segmented2)
    SegmentedGroup segmented2;
    @BindView(R.id.backBtn)
    ImageView backBtn;


    private Gson gson = new Gson();

    private List<MyErrorModel.MyTestpaperResultsBean> dataList = new ArrayList<>();
    private MyErrorSubjectAdapter myErrorSubjectAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_error_subject);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(MyErrorSubjectActivity.this, Color.WHITE, 1);
        StatusBarUtils.StatusBarLightMode(this);
        setTitle("我的错题");
        initView();

        initEvent();
        StatusBarUtils.StatusBarLightMode(this);
        loadData();

        backBtn.setOnClickListener(this);


    }

    private void loadData() {
        RequestParams params = new RequestParams();
        params.put("token", Constant.token);
        params.put("userId", Constant.uid);
        L.i("main", "token:::" + Constant.token + ":::" + Constant.uid);
        Async.post("my/quiz", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if (statusCode == 200) {
                    L.i("main", new String(responseBody));
//                    List<MyErrorModel> result = gson.fromJson(new String(responseBody), new TypeToken<List<MyErrorModel>>() {
//                    }.getType());//把JSON格式的字符串转为List
                    MyErrorModel jsonBean = gson.fromJson(new String(responseBody), MyErrorModel.class);
                    if (Tools.isIntNull(jsonBean.getSuccess()) == 1) {
                        dataList.addAll(jsonBean.getMyTestpaperResults());
                        myErrorSubjectAdapter.addRes(jsonBean.getMyTestpaperResults());

                    }
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

            }
        });
    }


    private void initEvent() {
        segmented2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                switch (i) {

                    case R.id.wodecuoti:

                        dataList.clear();
                        loadData();

                        break;

                    case R.id.tuijiancuoti:

                        ActivitySkipUtil.skipAnotherActivity(MyErrorSubjectActivity.this, MyErrorSubjectRecActivity.class, true);

                        break;
                }
            }
        });
    }

    private void initView() {

        segmented2.setTintColor(getResources().getColor(R.color.index_zhenti_btn_color));

        wodecuoti.setChecked(true);

        myErrorSubjectAdapter = new MyErrorSubjectAdapter(getApplicationContext(), dataList);
        listView.setAdapter(myErrorSubjectAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                HashMap map = new HashMap();
                map.put("id", dataList.get(position).getId());

                L.i("main", ":::::" + dataList.get(position).getId());

//                ActivitySkipUtil.skipAnotherActivity(MyErrorSubjectActivity.this, MyErrorDetailActivity.class, map);

                ActivitySkipUtil.skipAnotherActivity(MyErrorSubjectActivity.this, StErrorJxActivity.class, map);

            }
        });


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.backBtn:

                finish();
                break;
        }
    }
}
